package com.controller;

import java.util.*;

import com.domain.Comment;
import com.domain.Travel;
import com.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private TravelService travelService;


    @RequestMapping("/travelList")
    public String travelList(){
        List<Travel> travels= travelService.findAllTravel();
        System.out.println(travels);
        return "success";
    }

    @RequestMapping("/getOneTravel")
    public String getOneTravel(Integer travelid){
        Travel travel= travelService.findTravelById(travelid);
        System.out.println(travel);
        return "success";
    }

    @RequestMapping("/travelInfo")
    public ModelAndView travelInfo(Integer travelid){
        Travel travel= travelService.findTravelById(travelid);
        List<Comment> comments= travelService.findCommentsByTravelId(travelid);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("travel",travel);
        modelAndView.addObject("comments",comments);
        System.out.println(comments);
        modelAndView.setViewName("travelInfo");
        return modelAndView;
    }

    @RequestMapping("/commitComment")
    public String commitComment(Comment comment){
        Boolean result= travelService.commitOneComment(comment);
        if (result==true){
            return "success";
        }else {
            return "error";
        }
    }

    @RequestMapping("/commitTravel")
    public String commitTravel(Travel travel){
        Boolean result= travelService.commitOneTravel(travel);
        if (result==true){
            return "success";
        }else {
            return "error";
        }
    }

}
